import { useMemo } from 'react'
import useWidget from '@/hooks/useWidget'
import useChartDataInfo from '../useChartDataInfo'
import useColumns from './useColumns'
import useData from './useData'
import useScroll from './useScroll'
import useStyles from './useStyles'
import useSummary from './useSummary'

function useTable(widgetId) {
  const { currentWidget } = useWidget(widgetId)
  const chart = useMemo(() => currentWidget?.styleValue?.chart || {}, [currentWidget])
  const summary = useMemo(() => currentWidget?.styleValue?.summary || {}, [currentWidget])
  const { data, indexToFieldArray, keyToFieldObject } = useChartDataInfo(widgetId)

  const columns = useColumns(keyToFieldObject?.otherKey || [], chart)
  const dataSource = useData(data, indexToFieldArray)
  const scroll = useScroll(currentWidget)
  const styles = useStyles(chart)
  const _summary = useSummary(summary)

  return {
    columns,
    dataSource,
    scroll,
    styles,
    summary: _summary
  }
}

export default useTable
